package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;


import com.thinkfit.jy.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 安全锁脱落提示
 */
public class SafetyLockViewUtils {

    private static SafetyLockViewUtils safetyLockViewUtils;
    WindowManager windowManager;
    Context mContext;
    boolean isFist = true;
    View inflate;

    boolean isShow = false;

    Disposable mDisposableCountDown;
    TextView tvHint;
    private WindowManager.LayoutParams layoutParams;
    public static SafetyLockViewUtils getInstance(){
        if(safetyLockViewUtils == null){
            safetyLockViewUtils = new SafetyLockViewUtils();
        }
        return safetyLockViewUtils;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_safety_lock, null);
//            tvCount = inflate.findViewById(R.id.tvCount);
//            tvHint = inflate.findViewById(R.id.tv_hint);
        }

        if (layoutParams == null) {
            layoutParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);

            layoutParams.gravity = Gravity.CENTER;
        }
        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }
        isShow = true;
//        startCountDown();
    }

    public void startCountDown(){
        if(mDisposableCountDown != null){
            return;
        }
        closeDisposableCountDown();
        Observable.interval(5,  TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposableCountDown = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        goneLayout();
                    }

                    @Override
                    public void onError(Throwable e) {
                        closeDisposableCountDown();
                    }

                    @Override
                    public void onComplete() {
                        closeDisposableCountDown();
                    }
                });
    }

    public void closeDisposableCountDown(){
        if (mDisposableCountDown != null) {
            mDisposableCountDown.dispose();
            mDisposableCountDown = null;
        }
    }

    public void goneLayout(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
                closeDisposableCountDown();
                isShow = false;
            }
        }

    }

    public boolean isShow(){
        return isShow;
    }
    public void invaliView(){
        if(inflate != null){
            windowManager.removeView(inflate);
            closeDisposableCountDown();
            isFist = true;
            layoutParams = null;
            windowManager = null;
            inflate = null;
        }
    }

}
